﻿/*
创建人：zhangrj
日期：2022-08-9
描述：工步派工页面js
*/
var page = {
    pagetotal: 0,
    pageSize: 15,
    pageNumber: 1,
    rootPath: getRootPath(), //网站根路径
    url: getRootPath() + "/Ashx/ContainerStepInfoDispatchHandler.ashx", //后台处理逻辑url
    commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用类
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
        $("#ddlStatus").val("1"); //默认待派工状态
    },
    //页面事件绑定
    bind: function () {
        //主查询结果加载
        $("#gdContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            onSelect: function (index, rowData) {
                page.searchRBDataDetail(rowData);
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
		         { field: 'PRODUCTBATCH', title: '件号/批次号', width: 130 },
		         { field: 'PRODUCTDESCRIPTION', title: '零件名称', width: 130 },
                 { field: 'PLANQTY', title: '计划数', width: 40 },
                 { field: 'WIPQTY', title: '在制总数', width: 60 },
                 { field: 'WORKFLOWSTEPNAME', title: '当前工序', width: 180 },
                 { field: 'CONTAINERTYPENAME', title: '流水卡类型', width: 100 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始', width: 100, formatter: function (value) {
                     if (value !== null) {
                         return value.replace("T", "").replace("00:00:00", "").replace("0:00:00", "");
                     }
                 }
                 },
                 { field: 'PLANNEDCOMPLETIONDATE', title: '计划完成', width: 100, formatter: function (value) {
                     if (value !== null) {
                         return value.replace("T", "").replace("00:00:00", "").replace("0:00:00", "");
                     }
                 }
                 }
	           ]]
        });
        //批次号列表加载
        $("#gdContainerInfo2").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
		         { field: 'CONTAINERNAME', title: '流水卡号', width: 250 },
                 { field: 'WIPQTY', title: '在制数', width: 45 },
                 { field: 'FACONTAINERNAME', title: '主制批次', width: 150 },
                 { field: 'WORKFLOWID', title: '工艺ID', hidden: true }
	           ]]
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.searchData();
            }
        });
        $(document).ready(function () {
            //绑定分页go按钮
            $('#pageGo').on("click", function () {
                var pageNumber = $(this).parents('tr').find('.pagination-num').val();
                page.pageNumber = pageNumber;
                page.searchData();
            });
            //更改事件
            $("#txtScanContainer").textbox({ onChange: function (o, n) {
                page.searchData();
            }
            });
            //更改事件
            $("#txtScanMfgorder").textbox({ onChange: function (o, n) {
                page.searchData();
            }
            });
        });
        //查询按钮事件
        $('#btnSearch').on("click", function () {
            page.pageNumber = 1;
            page.searchData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
        //工步派工按钮
        $('#btnStepInfoDispatch').on("click", function () {
            clearMessage();
            page.dispatch();
        });
        //批次流水卡打印查看
        $('#btnViewContainer').on("click", function () {
            clearMessage();
            page.viewContainerPrint();
        });
        //文档查看按钮
        $('#pupDocumentView').on("click", function () {
            clearMessage();
            page.viewDoc();
        });
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        var param = {
            action: "GetContainerStepInfoDispatchList",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            containerName: $("#txtContainer").textbox('getValue'),
            scanContainerName: $("#txtScanContainer").textbox('getValue'),
            scanMfgorderName: $("#txtScanMfgorder").textbox('getValue'),
            productName: $("#txtProduct").textbox('getValue'),
            specNo: $("#txtSpecNo").textbox('getValue'),
            combinateName: $("#txtCombinate").textbox('getValue')
        };
        page.post(page.url, param, true, function (result) {
            if (!result.Result) {
                displayMessage(result.Message, false);
                return;
            }
            page.pagetotal = result.RowCount;
            $("#gdContainerInfo").datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        });
    },
    //查询批次明细（未启用）
    searchDataDetail: function (rowData) {
        clearMessage();
        clearEasyuiDataGrid("gdContainerInfo2", 1);
        var gridID = "#gdContainerInfo2";
        var params = [];
        var param = {
            togatherFlag: rowData.TOGETHERFLAG,
            productID: rowData.PRODUCTID,
            workflowStepID: rowData.WORKFLOWSTEPID,
            workflowID: rowData.WORKFLOWID,
            mfgOrderName: rowData.MFGORDERNAME,
            containerType: rowData.CONTAINERTYPE,
            containerNo: rowData.CONTAINERNO
        };
        params.push(param);

        var ajaxParam = { action: "GetContainerDetails", params: JSON.stringify(params) }
        page.post(page.url, ajaxParam, true, function (result) {
            if (!result.Result) {
                displayMessage(result.Message, false);
                return;
            }
            $(gridID).datagrid('loadData', { rows: result.DBTable });
            $(gridID).datagrid('checkAll');
        });
    },
    //查询热表批次明细
    searchRBDataDetail: function (rowData) {
        clearMessage();
        clearEasyuiDataGrid("gdContainerInfo2", 1);
        var params = {
            action: "GetRBContainerDetails",
            containerID:rowData.CONTAINERID,
            productBatch:rowData.PRODUCTBATCH
        };
        page.post(page.url, params, true, function (result) {
            if (!result.Result) {
                displayMessage(result.Message, false);
                return;
            }
            $("#gdContainerInfo2").datagrid('loadData', { rows: result.DBTable });
            $("#gdContainerInfo2").datagrid('checkAll');
        });
    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
        if (iscondition) {
            $('#txtScanContainer').textbox('clear');
            $('#txtScanMfgorder').textbox('clear');
            $('#txtProduct').textbox('clear');
            $('#txtContainer').textbox('clear');
            $('#txtSpecNo').textbox('clear');
        }
        if (isresult) {
            clearEasyuiDataGrid("gdContainerInfo", 1);
            clearEasyuiDataGrid("gdContainerInfo2", 1);
        }
    },
    //派工,批量派工条件:同工艺，同件号,同批次类型，同工序,(同可派数量?)
    dispatch: function () {
        var message = "";
        //  var selected = $("#gdContainerInfo2").datagrid("getChecked");
        var selected = $("#gdContainerInfo2").datagrid("getRows");
        var orgSelected = $("#gdContainerInfo").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("请选择批次", false);
            return;
        }
        if (orgSelected.length > 1) {
            displayMessage("请选择一条批次", false);
            return;
        }
        var url = page.rootPath + '/forms/Custom/htmlPage/ContainerStepInfoDispatchFormPage.html';
        if (page.isOper(selected)) {//如果存在暂停，则结束
            return;
        }
        //打开弹出页面
        url += "?v=" + new Date().getTime();
        url += "&parentDiv=dg";
        $('#dg').dialog({
            title: '生产派工',
            width: 1020,
            height: 800,
            top: 10,
            closed: false,
            cache: false,
            modal: true,
            content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
            poPupData: selected, //弹出页面的传参
            onClose: function () {
                var returnValue = window.returnValue;
                if (returnValue != null) {
                    if (returnValue.isSuccess == true) {
                        page.searchData();
                    }
                    displayMessage(returnValue.message, returnValue.isSuccess);
                }
            }
        });


    }
    //预览批次打印页面 add by zhangrj 2022-03-08
    , viewContainerPrint: function () {
        var selected = $("#gdContainerInfo2").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("请选择批次", false);
            return;
        }
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/CommonHandler.ashx",
            data: { action: "viewPrintPage", isReBiao: "1", params: JSON.stringify(selected) },
            cashe: false,
            async: true,
            success: function (data) {
                var result = JSON.parse(data);
                if (result.Result) {
                    var url = result.ReturnData + "?v=" + new Date().getTime();
                    window.open(url);
                } else {
                    displayMessage(result.Message, false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            }
        });
    }
    //验证是否有暂停批次 add by zhangrj 2022-04-18
    , isOper: function (selectedRows) {
        var isvaild = false;
        var param = [];
        for (var i = 0; i < selectedRows.length; i++) {
            param.push({ containerId: selectedRows[i].CONTAINERID, containerName: selectedRows[i].CONTAINERNAME });
        }
        $.ajax({
            type: "Post",
            url: page.commonUrl,
            data: { action: "GetOperReason", params: JSON.stringify(param) },
            cashe: false,
            async: false,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                if (data.Result === false) {
                    isvaild = true;
                    displayMessage(data.Message, data.Result);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                isvaild = true;
                alert(XMLHttpRequest.responseText);
            }
        });
        return isvaild;
    }
    //文档查看
    , viewDoc: function () {
        var selected = $("#gdContainerInfo2").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("请选择批次", false);
            return;
        }
        var url = page.rootPath + '/forms/Custom/' + "ProcessDocumentViewPopupForm.aspx?v=" + new Date().getTime();
        url += "&productId=" + selected[0].PRODUCTID + "&workflowId=" + selected[0].WORKFLOWID;
        $('#dg2').dialog({
            title: '文档查看',
            width: 730,
            height: 590,
            top: 10,
            closed: false,
            cache: false,
            modal: true,
            content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
            onClose: function () {

            }
        });
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
}
$(function () {
    page.init();
});